package org.jordens.jdbcspy.ui;

import org.jordens.jdbcspy.*;
import org.jordens.jdbcspy.sync.*;

import javax.management.remote.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
 * @author Adam Jordens
 */
public class JDBCSpyPanel extends JPanel
{
    /** whether or not automatic resets are enabled */
    private boolean enableAutomaticResets = false;

    /** Timer to control periodic panel resets */
    private Timer timer = null;

    /**
     * Constructs a new JDBCSpyPanel.
     */
    public JDBCSpyPanel()
    {
        // do nothing   
    }

    /**
     * @param host Hostname
     * @param port Port
     */
    public void connect(String host, int port)
    {
        JMXConnector connector = JMXUtilities.getJMXConnector(host, port);
        if (connector == null)
        {
            // unable to connect to host:port
            return;
        }

        JMXDataStreamer dataStreamer = new JMXDataStreamer(connector);
        connect(dataStreamer);
        
        timer = new Timer(60000, new ResetTask());
        timer.start();
    }

    /**
     * @param logFile Log file to display
     */
    public void connect(File logFile)
    {
        LogFileDataStreamer dataStreamer = new LogFileDataStreamer(logFile, 1000);
        connect(dataStreamer);
    }

    /**
     * @param dataStreamer Common data source for all visual panels.
     */
    private void connect(DataStreamer dataStreamer)
    {
        QueriesPerSecondPanel queriesPerSecondPanel =
                new QueriesPerSecondPanel(dataStreamer);

        QueriesTablePanel queriesTablePanel =
                new QueriesTablePanel(dataStreamer);

        dataStreamer.start();

        // Remove any previously displayed panels (in the event that we're re-connecting
        removeAll();
        revalidate();
        repaint();

        GridLayout gridLayout = new GridLayout(2,2);
        setLayout(gridLayout);

        add(queriesPerSecondPanel);
        add(queriesTablePanel);

        revalidate();
        repaint();

        if (timer != null)
        {
            // stop any previously running timer
            timer.stop();
        }
    }

    /**
     * Explicitly reset all components (that implement Resetable) on this panel.
     */
    public void reset()
    {
        for (Component c : getComponents())
        {
            if (c instanceof Resetable)
            {
                ((Resetable)c).reset();
            }
        }
    }

    /**
     * Periodically reset all panels (if enableAutomaticResets == true)
     */
    private class ResetTask implements ActionListener
    {
        /**
         * Constructs a new ResetTask.
         */
        public ResetTask()
        {
            // do nothing
        }

        /**
         * @param event ActionEvent
         */
        public void actionPerformed(ActionEvent event)
        {
            if (!enableAutomaticResets)
            {
                return;
            }
            
            reset();
        }
    }
}
